package net.towee.client.com.dashboard.view;

import net.towee.client.com.dashboard.presenter.GadgetPusherPresenter.Display;
import net.towee.client.gadgets.GadgetInfo;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.inject.Inject;

public class GadgetPusherView extends PopupPanel implements Display, HasOpenHandlers<GadgetInfo> {
	private final FlowPanel container;
	private final ClientResource resource;
	private final ToweeConstants constants;
	
	@Inject
	public GadgetPusherView(ToweeConstants constants, ClientResource resource) {
		super(true);
		this.constants = constants;
		this.resource = resource;
		this.container = new FlowPanel();
		
		setStyleName( resource.css().dashBoardGadgetPusher() );
		setAnimationEnabled( false );
		setGlassEnabled(true);
		setGlassStyleName( resource.css().loadingBarGlass() );
		setWidget(container);
	}
	
	@Override
	public void addAvailableGadgets(final GadgetInfo g, boolean installed) {
		FlowPanel item = new FlowPanel();
		item.getElement().setAttribute("item", "gadget");
		
		FlowPanel info = new FlowPanel();
		info.getElement().setAttribute("info", "");
		HTML name = new HTML( g.getName() );
		name.getElement().setAttribute("name", "");
		HTML desc = new HTML( g.getDescription() );
		desc.getElement().setAttribute("desc", "");
		info.add(name);
		info.add(desc);
		item.add(info);
		
		final Button operation = new Button( (installed)?constants.dashBoardGadgetInstalled():constants.dashBoardGadgetInstall() );
		operation.getElement().removeAttribute("class");
		operation.getElement().setAttribute("operation", (installed)?"installed":"install");
		item.add( operation );
		
		if(!installed) {
			operation.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					OpenEvent.fire(GadgetPusherView.this, g);
					operation.setText( constants.dashBoardGadgetInstalled() );
					operation.getElement().setAttribute("operation", "installed");
					operation.setEnabled(false);
				}
			});
		}
		
		container.add(item);
	}

	@Override
	public HasOpenHandlers<GadgetInfo> getAddGadgetEvents() {
		return this;
	}

	@Override
	public HandlerRegistration addOpenHandler(OpenHandler<GadgetInfo> handler) {
		return addHandler(handler, OpenEvent.getType());
	}

	@Override
	public void onGadgedAttached() {
		
	}

	@Override
	public void showPusher() {
		show();
		
		// Center it
		getElement().getStyle().setProperty("left", "50%");
		getElement().getStyle().setProperty("marginLeft", "-"+(getElement().getOffsetWidth()/2)+"px");
		getElement().getStyle().setProperty("top", "50%");
		getElement().getStyle().setProperty("marginTop", "-"+(getElement().getOffsetHeight()/2)+"px");
	}

}
